/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.treepoke.util.base;

import com.treepoke.util.dto.WebServiceDTO;
import com.treepoke.util.exception.BusinessException;
import com.treepoke.util.logging.CRCCLogger;
import com.treepoke.util.properties.TPConstant;
import com.treepoke.util.ws.SOAPRequestUtil;
import com.treepoke.util.ws.WSDispatchClient;
import javax.xml.soap.SOAPException;
import org.w3c.dom.Node;

/**
 *
 * @author dpulido
 */
public class TPBaseWSClient {
    
    protected CRCCLogger logger = CRCCLogger.getLogger(TPConstant.getNOMBRE_APLICACION());
    
   
    public <T> Object consumeWebService(WebServiceDTO webServiceDTO,
            Object[] params, Class<T> returnType, boolean collection)
            throws BusinessException {

        logger.debugMessage("Consumiendo Servicio Web "+webServiceDTO.getNombreOperacion());
        
        String requestMessage = SOAPRequestUtil.buildMessageSOAP(webServiceDTO, params);
        try {
            Node node = WSDispatchClient.invoke(webServiceDTO, requestMessage);
            return SOAPRequestUtil.getDataFromNode(node, returnType, BusinessException.class, null, collection);
        } catch (SOAPException e) {
            throw new BusinessException(e.getMessage(), e, 0);
        }
    }
}
